﻿using IOP.ISEA;
using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace VkSample66
{
    [ModulePriority(ModulePriority.Assets)]
    public class AssetsModule : VulkanModule, IVulkanRenderWapperModule
    {

        public AssetsModule()
        {
        }

        public VulkanRenderWapper RenderWapper { get; set; }

        protected override async Task Load(VulkanGraphicsManager graphicsManager)
        {
            graphicsManager.TryGetRenderGroup("EarthGroup", out IRenderGroup earthGroup);
            graphicsManager.TryGetRenderGroup("MoonGroup", out IRenderGroup moonGroup);
            graphicsManager.TryGetRenderGroup("StarGroup", out IRenderGroup starGroup);

            var earthArchetype = ContextManager.CreateArchetype(new TransformComponent(), new EarthComponent());
            var moonArchetype = ContextManager.CreateArchetype(new TransformComponent(), new MoonComponent());
            var starArchetype = ContextManager.CreateArchetype(new TransformComponent(), new StarComponent());
            var archetype2 = ContextManager.CreateArchetype(new LightComponent());

            ISEADocument document = new ISEADocument();
            await document.Load(Path.Combine(AppContext.BaseDirectory, "Test66.isea"));
            var renders = await graphicsManager.CreateRenderObjectsFromISEA<RenderObject>(document);
            var light = graphicsManager.CreateParallelLight<RenderObject>("main", new Vector3(0, 100, 100), new LightStrength(0.9f, 0.9f, 0.5f, 0.8f));
            ContextManager.CreateContexts(1, starArchetype, starGroup, renders.Where(x => x.Name == "SmallSky").First());
            ContextManager.CreateContexts(1, starArchetype, starGroup, renders.Where(x => x.Name == "BigSky").First());
            ContextManager.CreateContexts(1, earthArchetype, earthGroup, renders.Where(x => x.Name == "Earth").First());
            ContextManager.CreateContexts(1, moonArchetype, moonGroup, renders.Where(x => x.Name == "Moon").First());
            ContextManager.CreateContexts(1, archetype2, earthGroup, light);
            ContextManager.CreateContexts(1, archetype2, moonGroup, light);
        }

        protected override Task Unload(VulkanGraphicsManager manager)
        {
            return Task.CompletedTask;
        }
    }
}
